package huzh.foundation.log;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;

import huzh.foundation.startup.AppEnv;

/**
 *<p>title:LogMgr</p>
 *<p>description:</p>
 * @since 2014-5-19
 * @author huzh
 * @version 1.0
 */
public class LogMgr {
    private static boolean isInit = false;

    @SuppressWarnings("unchecked")
    public static void debug(Class paramClass, String paramString){
        if (AppEnv.isDebug()) {
            System.out.println(paramString);
        }
        getLog(paramClass).debug(paramString);
    }

    @SuppressWarnings("unchecked")
    public static void error(Class paramClass, String paramString) {
        if (AppEnv.isDebug()) {
            System.out.println(paramString);
        }
        getLog(paramClass).error(paramString);
    }

    @SuppressWarnings("unchecked")
    public static void fatal(Class paramClass, String paramString) {
        if (AppEnv.isDebug()) {
            System.out.println(paramString);
        }
        getLog(paramClass).fatal(paramString);
    }

    @SuppressWarnings("unchecked")
    public static Log getLog(Class paramClass){
        return LogFactory.getLog(paramClass);
    }

    @SuppressWarnings("unchecked")
    public static void info(Class paramClass, String paramString){
        if (AppEnv.isDebug()) {
            System.out.println(paramString);
        }
        getLog(paramClass).info(paramString);
    }

    public static synchronized boolean init(String paramString){
        if (isInit)
            return true;
        try{
            System.setProperty("org.apache.commons.logging.LogFactory", "org.apache.commons.logging.impl.Log4jFactory");
            System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JCategoryLog");

            PropertyConfigurator.configure(paramString);
            System.out.println("Success Init Log !!!");
            isInit = true;
            return true;
        }catch (Exception localException) {
            System.out.println("Failed Init Log !!!");
            System.out.println(localException.getMessage());
            localException.printStackTrace();
        }
        return false;
    }

    @SuppressWarnings("unchecked")
    public static void warn(Class paramClass, String paramString){
        if (AppEnv.isDebug()) {
            System.out.println(paramString);
        }
        getLog(paramClass).warn(paramString);
    }
}
